//
//  User.m
//  Supervisor
//
//  Created by 小宇 on 2017/5/12.
//  Copyright © 2017年 北京怀教网络科技股份有限公司. All rights reserved.
//

#import "User.h"

@interface User ()
@property (nonatomic,assign)LoginState loginstate;
@property (nonatomic,copy)NSString *userName;
@property (nonatomic,copy)NSString *passWord;
@property (nonatomic,copy)NSString *accessToken;
@property (nonatomic,copy)NSString *userId;
@property (nonatomic,copy)NSString *userFace;
@property (nonatomic,copy)NSString *userOrignName;
@end

@implementation User


+ (instancetype)shareInstance{
    static User * instance = nil;
    static dispatch_once_t tocken ;
    
    dispatch_once(&tocken, ^{
        
        instance = [[User alloc] init];
    });
    
    return instance;
}

- (void)loginOut{
    self.loginstate = LoginOut;
    [kUserDefaults setBool:NO forKey:@"loginState"];
    [kUserDefaults synchronize];
}

- (void)loginIn{
    self.loginstate = LoginIn;
    [kUserDefaults setBool:YES forKey:@"loginState"];
    [kUserDefaults synchronize];
}

- (LoginState)getLoginState{
    if ([kUserDefaults boolForKey:@"loginState"] == YES) {
        self.loginstate = LoginIn;
    }else{
        self.loginstate = LoginOut;
    }
    return self.loginstate;
}

- (User *)getUser{
    NSData *data = [kUserDefaults objectForKey:@"user"];
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    User *user = [unarchiver decodeObjectForKey:@"user"];
    [unarchiver finishDecoding];
    return user;
}

- (NSString *)getUserAccessToken{
    User *user = [self getUser];
    return user.accessToken;
}

- (NSString *)getUserName{
    User *user = [self getUser];
    return user.userName;
}

- (NSString *)getUserOrignName{
    User *user = [self getUser];
    return user.userOrignName;
}

- (NSString *)getUserFace{
    User *user = [self getUser];
    return user.userFace;
}

- (void)removeUserName{
    User *user = [self getUser];
    user.userName = @"";
}

- (NSString *)getPassWord{
    User *user = [self getUser];
    return user.passWord;
}

- (void)changePassWord:(NSString *)pwd{
    [self setUserName:[self getUserName] passWord:pwd accessToken:[self getUserAccessToken] userId:[self getUserId] userFace:[self getUserFace] userOrignName:[self getUserOrignName]];
}

- (void)changeUserFace:(NSString *)userface{
    [self setUserName:[self getUserName] passWord:[self getPassWord] accessToken:[self getUserAccessToken] userId:[self getUserId] userFace:userface userOrignName:[self getUserOrignName]];
}

- (NSString *)getUserId{
    User *user = [self getUser];
    return user.userId;
}

- (void)setUserName:(NSString *)userName passWord:(NSString *)PassWord accessToken:(NSString *)accessToken userId:(NSString *)userId userFace:(NSString *)userFace userOrignName:(NSString *)userOrignName{
    self.userName = userName;
    self.passWord = PassWord;
    self.accessToken = accessToken;
    self.userId = userId;
    self.userFace = userFace;
    self.userOrignName = userOrignName;
    NSMutableData *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:self forKey:@"user"];
    [archiver finishEncoding];
    [kUserDefaults setObject:data forKey:@"user"];
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.userName forKey:@"userName"];
    [aCoder encodeObject:self.passWord forKey:@"passWord"];
    [aCoder encodeObject:self.accessToken forKey:@"accessToken"];
    [aCoder encodeObject:self.userId forKey:@"userId"];
    [aCoder encodeObject:self.userFace forKey:@"userFace"];
    [aCoder encodeObject:self.userOrignName forKey:@"userOrignName"];
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self.userName = [aDecoder decodeObjectForKey:@"userName"];
    self.passWord = [aDecoder decodeObjectForKey:@"passWord"];
    self.accessToken = [aDecoder decodeObjectForKey:@"accessToken"];
    self.userId =[aDecoder decodeObjectForKey:@"userId"];
    self.userFace =[aDecoder decodeObjectForKey:@"userFace"];
    self.userOrignName =[aDecoder decodeObjectForKey:@"userOrignName"];
    return self;
}


@end
